<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends Controller {
    public function register(){
        $this->display();
    }
    public function do_register(){
        $User = D("User");
        if (!$User->create()){
            $this->error($User->getError());
        } else{
            $User->add();
            $this->success("注册成功");
        }
    }

    public function login(){
        $this->display();
    }
    public function do_login(){
        $data['username'] = I("post.username");
        $data['password'] = md5(substr(md5(I("post.password")), -6, 6));
        $User = M('User');
        $user=$User->where($data)->find();
        if (!$user){
            $this->error("用户名或者密码错误");
        }
        

        //session超全局变量
        $_SESSION['user']=$user;
        $this->success('登录成功',U('Index/index'));

    }
    public function logout(){
        $_SESSION=array();

        //session_name(),cookie中存储的session标识，既key
        if (isset($_COOKIE[session_name()])){
            setcookie(session_name(), "", time()-3600,"/");
        }
        $this->success('你已经安全退出',U("User/login"));



    }
}